local skill = fk.CreateSkill{
  name = "tea__piaobo",
  tags = { Skill.Compulsory }
}
local TeaU = require "packages.wild_collection.utility.utility"
local getCountry = function(player)
  if player:getMark("@tea__piaobo_mark") ~= 0 then
    return player:getMark("@tea__piaobo_mark")
  else
    return "china"
  end
end
local changeCountry = function(room, player)
  local mark = getCountry(player)
  local from, to
  if mark == "china" then
    mark = "japan"
    from = "china"
    to = "japan"
  elseif mark == "japan" then
    mark = "china"
    from = "japan"
    to = "china"
  end
  local event_data = {
    from = from,
    to = to,
    target = player,
  }
  room:setPlayerMark(player, "@tea__piaobo_mark", mark)
  room:sendLog{
    type = "#EnterOneCountry",
    from = player.id,
    arg = from,
    arg2 = to,
  }
  room.logic:trigger(TeaU.AfterCountryChanged, player, event_data)
  return {from, to}
end
---@type TrigSkelSpec<TrigFunc>
local piaobo = {
  mute = true,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skill.name, true)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:notifySkillInvoked(player, skill.name, "special")
    for _, p in ipairs(room.alive_players) do
      room:setPlayerMark(p, "@tea__piaobo_mark", "china")
    end
    changeCountry(room, player)
  end,
}
---@type TrigSkelSpec<TrigFunc>
local piaobo_trigger = {
  mute = true,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skill.name, true, true) and target.phase == Player.Start and not target:isNude()
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local cid = room:askToDiscard(target, {
      min_num = 1,
      max_num = 1,
      skill_name = skill.name,
      prompt = "#tea__piaobo-invoke",
      include_equip = true,
      skip = true,
    })
    if #cid > 0 then
      event:setCostData(self, cid)
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:notifySkillInvoked(player, skill.name, "special")
    if getCountry(target) == "china" then
      player:broadcastSkillInvoke(skill.name, 2)
    else
      player:broadcastSkillInvoke(skill.name, 1)
    end
    room:throwCard(event:getCostData(self), skill.name, target, target)
    if not target.dead then
      changeCountry(room, target)
    end
  end,
}
---@type ProhibitSpec
local piaobo_prohibit = {
  frequency = Skill.Compulsory,
  is_prohibited = function(self, from, to)
    return getCountry(from) ~= getCountry(to)
  end,
}
---@type DistanceSpec
local piaobo_distance = {
  frequency = Skill.Compulsory,
  correct_func = function(self, from, to)
    local arr1 = {}
    local arr2 = {}
    local p = from:getNextAlive()
    for i = 1, 15 do
      if p == to or p == from then break end
      table.insert(arr1, p)
      p = p:getNextAlive()
    end
    p = from:getLastAlive()
    for i = 1, 15 do
      if p == to or p == from then break end
      table.insert(arr2, p)
      p = p:getLastAlive()
    end
    local find1 = table.filter(arr1, function(p1)
      return getCountry(p1) ~= getCountry(from)
    end)
    local find2 = table.filter(arr2, function(p2)
      return getCountry(p2) ~= getCountry(from)
    end)
    if #arr1 < #arr2 then
      return - #find1
    elseif #arr1 > #arr2 then
      return  - #find2
    else
      return -(math.max(#find1, #find2))
    end
  end,
}
skill:addEffect(fk.GameStart, piaobo)
skill:addEffect(fk.EventPhaseStart, piaobo_trigger)
skill:addEffect("prohibit", piaobo_prohibit)
skill:addEffect("distance", piaobo_distance)
return skill